const Base = require('./base.js');

module.exports = class extends Base {
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    delete query.page;
    delete query.size;

    const data = await this.model('utility_list')
      .where(query)
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }

  async putTagAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('tag_list');

    if (id > 0) {
      await model.where({ id: id }).update(values);
    } else {
      delete values.id;
      values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];
      values.type = 1;

      await model.add(values);
    }
    return this.success(values);
  }

  async putFuliAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('tag_list');

    if (id > 0) {
      await model.where({ id: id }).update(values);
    } else {
      delete values.id;
      values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];
      values.type = 2;

      await model.add(values);
    }
    return this.success(values);
  }

  async delAction() {
    const id = this.get('id');
    await this.model('tag_list')
      .where({ id: id })
      .limit(1)
      .delete();

    return this.success();
  }

  async listAllAction() {
    const query = this.get();
    const data = await this.model('tag_list')
      .where(query)
      .select();
    return this.success(data);
  }
};
